package com.xy.tx;

import com.tencentcloudapi.common.profile.Region;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.IdCardVerificationRequest;
import com.tencentcloudapi.faceid.v20180301.models.IdCardVerificationResponse;
import com.tencentcloudapi.faceid.v20180301.models.ImageRecognitionRequest;
import com.tencentcloudapi.faceid.v20180301.models.ImageRecognitionResponse;
import com.tencentcloudapi.iai.v20200303.IaiClient;
import com.tencentcloudapi.iai.v20200303.models.*;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.MLIDPassportOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.MLIDPassportOCRResponse;
import com.tencentcloudapi.ocr.v20181119.models.MainlandPermitOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.MainlandPermitOCRResponse;
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 腾讯相关接口
 * @author guojian
 */
@Slf4j
public class TxCommon {
	/**
	 * 腾讯二要素认证
	 *
	 * @param
	 */
	public static IdCardVerificationResponse idCardVerification(String userName,String idCard) {
		IdCardVerificationRequest req = new IdCardVerificationRequest();
		// 实例化要请求产品的client对象,clientProfile是可选的
		FaceidClient client = new FaceidClient(TxClient.cred,  Region.Shanghai.getValue(), TxClient.clientProfile);
		req.setIdCard(idCard);
		req.setName(userName);
		// 返回的resp是一个IdCardVerificationResponse的实例，与请求对象对应
		IdCardVerificationResponse resp = null;
		try {
			resp = client.IdCardVerification(req);
			log.info("请求腾讯二要素结果返回：{}", IdCardVerificationResponse.toJsonString(resp));
		} catch (Exception e) {
			log.error("请求腾讯二要素接口超时、跳过认证",e);

		}
		return resp;
	}
	/**
	 * 腾讯三要素认证
	 *
	 * @param
	 */
	public static ImageRecognitionResponse imageRecognition(String userName,String idCard, String filePath) {
		FaceidClient client = new FaceidClient(TxClient.cred, Region.Shanghai.getValue(), TxClient.clientProfile);
		// 实例化一个请求对象,每个接口都会对应一个request对象
		ImageRecognitionRequest req = new ImageRecognitionRequest();

		req.setName(userName);
		req.setIdCard(idCard);
		req.setImageBase64(getPictureBase(filePath));
		ImageRecognitionResponse resp = null;
		try {
			resp = client.ImageRecognition(req);
			log.info("请求腾讯三要素结果返回：{}", ImageRecognitionResponse.toJsonString(resp));
		} catch (Exception e) {
			log.error("请求腾讯三要素接口超时、跳过认证",e);

		}
		return resp;
	}
	/**
	 * 国外护照识别
	 */
	public static MLIDPassportOCRResponse  mlidpassportocr(String filePath){
		// 实例化要请求产品的client对象,clientProfile是可选的
		OcrClient client = new OcrClient(TxClient.cred, Region.Guangzhou.getValue(), TxClient.clientProfile);
		// 实例化一个请求对象,每个接口都会对应一个request对象
		MLIDPassportOCRRequest req = new MLIDPassportOCRRequest();
		req.setImageBase64(getPictureBase(filePath));
		//为true返回人脸照片
		req.setRetImage(true);
		try {
		 MLIDPassportOCRResponse resp = client.MLIDPassportOCR(req);
			log.info("请求腾讯 国外护照识别 返回参数{}",MLIDPassportOCRResponse.toJsonString(resp));
		 return resp;
		} catch (Exception e) {
			log.error("请求腾讯国外护照接口超时、跳过认证",e);

		}
		return  null;
	}
	/**
	 * 港澳台来往内地通行证识别
	 */
	public static MainlandPermitOCRResponse  mainlandPermitOCR(String filePath){
		// 实例化要请求产品的client对象,clientProfile是可选的
		OcrClient client = new OcrClient(TxClient.cred, Region.Shanghai.getValue(), TxClient.clientProfile);
		// 实例化一个请求对象,每个接口都会对应一个request对象
		MainlandPermitOCRRequest req = new MainlandPermitOCRRequest();
		req.setRetProfile(true);
		//为true返回人脸照片
		req.setImageBase64(getPictureBase(filePath));
		try {
			MainlandPermitOCRResponse resp = client.MainlandPermitOCR(req);
			log.info("请求腾讯 港澳台来往内地通行证识别 返回参数{}",MainlandPermitOCRResponse.toJsonString(resp));
			return resp;
		} catch (Exception e) {
			log.error("请求腾讯国外护照接口超时、跳过认证",e);

		}
		return  null;
	}


	/**
	 * 人脸对比
	 */
	public static CompareFaceResponse compareFace(String imageABase64 ,String filePath){
		// 实例化要请求产品的client对象,clientProfile是可选的
		IaiClient client = new IaiClient(TxClient.cred, Region.Shanghai.getValue(), TxClient.clientProfile);
		// 实例化一个请求对象,每个接口都会对应一个request对象
		CompareFaceRequest req = new CompareFaceRequest();
		req.setImageA(imageABase64);
		req.setImageB(getPictureBase(filePath));
		try {
			CompareFaceResponse resp = client.CompareFace(req);
			log.info("请求腾讯 人脸对比 返回参数{}",CompareFaceResponse.toJsonString(resp));
			return resp;
		} catch (Exception e) {
			log.error("请求腾讯人脸对比接口超时、跳过认证",e);

		}
		return  null;
	}

	/**
	 * 人脸识别
	 */
	public static SearchPersonsResponse searchPersons(String filePath ,String [] groupId ,Float faceMatchThreshold){
		// 实例化要请求产品的client对象,clientProfile是可选的
		IaiClient client = new IaiClient(TxClient.cred, Region.Shanghai.getValue(), TxClient.clientProfile);
		// 实例化一个请求对象,每个接口都会对应一个request对象
		SearchPersonsRequest req = new SearchPersonsRequest();
		req.setMaxPersonNum(1L);
		req.setImage(getPictureBase(filePath));
		req.setGroupIds(groupId);
		req.setFaceMatchThreshold(faceMatchThreshold);
		try {
			SearchPersonsResponse resp = client.SearchPersons(req);
			log.info("请求腾讯 人脸识别 返回参数{}",SearchPersonsResponse.toJsonString(resp));
			return resp;
		} catch (Exception e) {
			log.error("请求腾讯人脸对比接口超时、跳过认证",e);

		}
		return  null;
	}

	/**
	 * 人员库新增
	 */
	public static CreateGroupResponse createGroup(String groupName ,String groupId){
		// 实例化要请求产品的client对象,clientProfile是可选的
		IaiClient client = new IaiClient(TxClient.cred, Region.Shanghai.getValue(), TxClient.clientProfile);
		// 实例化一个请求对象,每个接口都会对应一个request对象
		CreateGroupRequest req = new CreateGroupRequest();
		req.setGroupId(groupId);
		req.setGroupName(groupName);
		try {
			CreateGroupResponse resp = client.CreateGroup(req);
			log.info("请求腾讯 人员库新增 返回参数{}",CreateGroupResponse.toJsonString(resp));
			return resp;
		} catch (Exception e) {
			log.error("请求腾讯 人员库新增 接口超时、跳过认证",e);
		}
		return  null;
	}
	/**
	 * 人员新增
	 */
	public static CreatePersonResponse createPerson(String filePath ,String groupId,String personId,String personName){
		// 实例化要请求产品的client对象,clientProfile是可选的
		IaiClient client = new IaiClient(TxClient.cred, Region.Shanghai.getValue(), TxClient.clientProfile);
		// 实例化一个请求对象,每个接口都会对应一个request对象
		CreatePersonRequest req = new CreatePersonRequest();
		req.setGroupId(groupId);
		req.setImage(getPictureBase(filePath));
		req.setPersonId(personId);
		req.setPersonName(personName);
		try {
			CreatePersonResponse resp = client.CreatePerson(req);
			log.info("请求腾讯 人员新增 返回参数{}",CreatePersonResponse.toJsonString(resp));
			return resp;
		} catch (Exception e) {
			log.error("请求腾讯 人员新增 接口超时、跳过认证 ",e);
		}
		return  null;
	}
	/**
	 * 人员删除
	 */
	public static DeletePersonFromGroupResponse deletePersonFromGroup(String personId,String groupId){
		// 实例化要请求产品的client对象,clientProfile是可选的
		IaiClient client = new IaiClient(TxClient.cred, Region.Shanghai.getValue(), TxClient.clientProfile);
		// 实例化一个请求对象,每个接口都会对应一个request对象
		DeletePersonFromGroupRequest req = new DeletePersonFromGroupRequest();
		req.setPersonId(personId);
		req.setGroupId(groupId);
		try {
			DeletePersonFromGroupResponse resp = client.DeletePersonFromGroup(req);
			log.info("请求腾讯 人员删除 返回参数{}",DeletePersonFromGroupResponse.toJsonString(resp));
			return resp;
		} catch (Exception e) {

			log.error("请求腾讯 人员删除 接口超时、跳过认证",e);

		}
		return  null;
	}

	/**
	 * 人员检测
	 */
	public static DetectFaceResponse detectFace(String filePath){
		// 实例化要请求产品的client对象,clientProfile是可选的
		IaiClient client = new IaiClient(TxClient.cred, Region.Shanghai.getValue(), TxClient.clientProfile);
		// 实例化一个请求对象,每个接口都会对应一个request对象
		DetectFaceRequest req = new DetectFaceRequest();
		req.setImage(getPictureBase(filePath));
		req.setNeedQualityDetection(1L);
		try {
			DetectFaceResponse resp = client.DetectFace(req);
			log.info("请求腾讯 人员检测 返回参数{}",DetectFaceResponse.toJsonString(resp));
			return resp;
		} catch (Exception e) {
			log.error("请求腾讯 人员检测 接口超时、跳过认证",e);

		}
		return  null;
	}
	/**
	 * 人员检测
	 */
	public static GetPersonListResponse getPersonList(String groupId,Long offset,Long limit){
		// 实例化要请求产品的client对象,clientProfile是可选的
		IaiClient client = new IaiClient(TxClient.cred, Region.Shanghai.getValue(), TxClient.clientProfile);
		// 实例化一个请求对象,每个接口都会对应一个request对象
		GetPersonListRequest req = new GetPersonListRequest();
		req.setGroupId(groupId);
		req.setLimit(limit);
		req.setOffset(offset);
		try {
			GetPersonListResponse resp = client.GetPersonList(req);
			log.info("请求腾讯 人员检测 返回参数{}",DetectFaceResponse.toJsonString(resp));
			return resp;
		} catch (Exception e) {
			log.error("请求腾讯 人员检测 接口超时、跳过认证",e);

		}
		return  null;
	}

	/**
	 * 人员搜索
	 */
	public static GetPersonBaseInfoResponse getPersonInfo(String personId){
		// 实例化要请求产品的client对象,clientProfile是可选的
		IaiClient client = new IaiClient(TxClient.cred, Region.Shanghai.getValue(), TxClient.clientProfile);
		// 实例化一个请求对象,每个接口都会对应一个request对象
		GetPersonBaseInfoRequest req = new GetPersonBaseInfoRequest();
		req.setPersonId(personId);


		try {
			GetPersonBaseInfoResponse resp = client.GetPersonBaseInfo(req);
			log.info("请求腾讯 人员搜索 返回参数{}",DetectFaceResponse.toJsonString(resp));
			return resp;
		} catch (Exception e) {
			log.error("请求腾讯 人员搜索 接口超时、跳过认证");


		}
		return  null;
	}


	/**
	 * 将图片转换成Base64编码
	 *
	 * @param filePath 待处理图片地址
	 * @return
	 */
	public static String getPictureBase(String filePath) {
		//获取图片流地址
		InputStream in = null;
		final ByteArrayOutputStream data = new ByteArrayOutputStream();
		//读取图片字节数组
		try {
			URL url = new URL(filePath);
			final byte[] by = new byte[1024];
			// 创建链接获取图片
			final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(5000);
			in = conn.getInputStream();
			int len = -1;
			while ((len = in.read(by)) != -1) {
				data.write(by, 0, len);
			}
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//对字节数组Base64编码
		BASE64Encoder encoder = new BASE64Encoder();
		//返回Base64编码过的字节数组字符串
		String encode = encoder.encode(data.toByteArray());
		encode = encode.replaceAll("[\\s*\t\n\r]", "");
		return encode;
	}

}
